<?php
/**
 * @title 399. 除法求值 - BFS + 穷列
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $equations
     * @param Float[] $values
     * @param String[][] $queries
     * @return Float[]
     */
    function calcEquation($equations, $values, $queries) {
        /**
         * @since 2024-01-11 所有已知的点
         */
        $points = [];

        /**
         * @since 2024-01-11 穷列所有除法
         */
        $map = [];

        /**
         * @since 2024-01-11 遍历已知除法
         */
        foreach ($equations as $key => $equation) {
            /**
             * @since 2024-01-11 记录除法
             */
            $map[$equation[0]][$equation[1]] = $values[$key];
            $map[$equation[1]][$equation[0]] = 1 / $values[$key];

            /**
             * @since 2024-01-11 记录点
             */
            $points[$equation[0]] = $equation[0];
            $points[$equation[1]] = $equation[1];
            $map[$equation[0]][$equation[0]] = 1;
            $map[$equation[1]][$equation[1]] = 1;
        }

        /**
         * @since 2024-01-11 穷列所有点
         */
        while($points){
            /**
             * @since 2024-01-11 pop一个点
             */
            $p = array_pop($points);

            /**
             * @since 2024-01-11 记录所有访问过的点
             */
            $visited = [];

            /**
             * @since 2024-01-05 BFS广度遍历
             */
            $nodeArray = [
                [$p, []],
            ];
            while($nodeArray){
                /**
                 * @since 2024-01-05 下次遍历元素的集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2024-01-05 遍历当前元素集合
                 */
                foreach ($nodeArray as list($node, $pathArray)){
                    /**
                     * @since 2024-01-11 记录已经访问的点
                     */
                    $visited[$node] = true;

                    /**
                     * @since 2024-01-11 遍历当前元素的所有除法
                     */
                    foreach ($map[$node] as $nextNode => $v){
                        /**
                         * @since 2024-01-11 如果下一个元素没有访问过
                         */
                        if(!isset($visited[$nextNode])){
                            /**
                             * @since 2024-01-11 记录已访问
                             */
                            $visited[$nextNode] = true;

                            /**
                             * @since 2024-01-11 遍历路径上除法
                             */
                            foreach ($pathArray as $pathNode) {
                                /**
                                 * @since 2024-01-11 记录下一个元素的除法
                                 */
                                $map[$pathNode][$nextNode] = $map[$pathNode][$node] * $v;
                                $map[$nextNode][$pathNode] = 1 / $map[$pathNode][$node] / $v;
                            }
                            $pathArray[] = $node;

                            /**
                             * @since 2024-01-11 下次遍历的元素
                             */
                            $nextNodeArray[] = [$nextNode, $pathArray];
                        } else {}
                    }
                }

                /**
                 * @since 2024-01-05 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }
        }

        /**
         * @since 2024-01-11 所有问题结果
         */
        $result = [];

        /**
         * @since 2024-01-11 遍历所有问题
         */
        foreach ($queries as $query) {
            /**
             * @since 2024-01-11 如果问题的两个元素都存在
             */
            if(isset($map[$query[0]][$query[1]])){
                /**
                 * @since 2024-01-11 记录结果
                 */
                $result[] = $map[$query[0]][$query[1]];
            } else {
                /**
                 * @since 2024-01-11 记录结果
                 */
                $result[] = -1;
            }
        }

        /**
         * @return Float[]
         */
        return $result;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.11_3.php
 */
$datas = [
    // [[["a","b"],["b","c"]], [2.0,3.0], [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]],
    [[["x1","x2"],["x2","x3"],["x1","x4"],["x2","x5"]], [3.0,0.5,3.4,5.6], [["x1","x3"]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->calcEquation(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
